﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class User11 : Form
    {
        public User11()
        {
            InitializeComponent();
            LoadMessages();  // 加载消息
        }

        // 加载用户消息并在 DataGridView 中展示
        private void LoadMessages()
        {
            dataGridView1.Rows.Clear();  // 清空旧数据
            Dao dao = new Dao();
            string sql = $"select messageid, title, senderid, sendtime, isread from message where receiverid = {Data.UID}";
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                // 获取消息状态
                string isRead = dc[4].ToString(); // 获取是否已读状态
                int rowIndex = dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), isRead);

                // 根据是否已读设置行的颜色
                if (isRead == "未读")
                {
                    dataGridView1.Rows[rowIndex].DefaultCellStyle.ForeColor = Color.Red; // 设置未读行的背景颜色为红色
                }
            }
            dc.Close();
            dao.DaoClose();
        }


        // 单击消息时，显示详细内容
        private void dataGridViewMessages_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                // 获取选中的消息 ID
                int messageId = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

                // 加载消息详细内容
                ShowMessageDetails(messageId);
            }
        }

        // 显示消息的详细内容
        private void ShowMessageDetails(int messageId)
        {
            Dao dao = new Dao();
            string sql = $"select title, content, senderid, sendtime, isread from message where messageid = {messageId}";
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                string title = dc[0].ToString();
                string content = dc[1].ToString();
                string sender = dc[2].ToString();
                string sendtime = dc[3].ToString();
                string isread = dc[4].ToString();
                //dataGridView1.Rows.Add(title, content, sender, sendtime, isread);
                user12 user = new user12();
                OpenForm(user);
                user.SetLabelText(title, content, sender, sendtime);
                if (isread == "未读")
                {
                    string sql2 = $"update message set isread = '已读' where messageid = {messageId}";
                    IDataReader dc2 = dao.read(sql2);
                    dc2.Close();
                    LoadMessages();
                }
            }
            
            dc.Close();
            dao.DaoClose();
        }

        private void OpenForm(Form subForm)
        {
            foreach (Control item in this.panel1.Controls)
            {
                if (item is Form)
                {
                    ((Form)item).Close();
                }
            }
            subForm.TopLevel = false;// 将子窗体设置为非顶级控件
            subForm.FormBorderStyle = FormBorderStyle.None;//设置无边框
            subForm.Parent = this.panel1;//设置窗体容器
            subForm.Dock = DockStyle.Fill; //容器大小随着调整窗体大小自动变化
            subForm.Show();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            dataGridView1.ClearSelection();  //取消第一行默认选中状态
        }
    }
}
